<template>
	<l-page :page.sync="queryParams.pageNum" :page-size.sync="queryParams.pageSize" :total="total" @getList="getList">
		<el-form ref="queryForm" slot="header" :inline="true" :model="queryParams" label-width="75px">
			<el-form-item label="版本号:" prop="versionCode">
				<el-input v-model.trim="queryParams.versionCode" clearable placeholder="请输入版本号" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<start-end-time ref="child" :get-time="getTime" title="创建日期:"></start-end-time>
		</el-form>

		<template slot="options">
			<l-search @search="handleQuery" @refresh="resetQuery" />
			<el-button v-hasPermi="['system:version:add']" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
		</template>

		<el-table v-loading="loading" :data="versionList" stripe>
			<el-table-column align="center" label="序号" type="index" width="55" />
			<el-table-column :formatter="emptyToString" align="center" label="版本号" min-width="120" prop="versionCode" />
			<el-table-column :formatter="emptyToString" align="center" label="版本名称" min-width="120" prop="versionName" />
			<el-table-column :formatter="emptyToString" align="center" label="版本信息" min-width="120" prop="versionInfo" />
			<el-table-column :formatter="emptyToString" align="center" label="备注" min-width="120" prop="remark" show-overflow-tooltip />
			<el-table-column :formatter="emptyToString" align="center" label="强制下载" prop="status" min-width="156">
				<template slot-scope="scope">
					<el-switch
						v-model="scope.row.updateType"
						active-value="forcibly"
						inactive-value="solicit"
						active-text="是"
						class="switch"
						inactive-text="否"
						@change="statusChange($event, scope.row.id)"
					></el-switch>
				</template>
			</el-table-column>
			<el-table-column :formatter="emptyToString" align="center" label="创建时间" min-width="200" prop="createTime">
				<template slot-scope="scope">
					<div class="w-full flex flex-col items-center">
						<span>{{ dayjs(scope.row.createTime).format('YYYY-MM-DD') }}</span>
						<span>{{ dayjs(scope.row.createTime).format('hh:mm:ss') }}</span>
					</div>
				</template>
			</el-table-column>
			<el-table-column :width="tableAction" align="center" fixed="right" header-align="center" label="操作">
				<template slot-scope="scope">
					<div class="table-action">
						<el-button v-hasPermi="['system:version:edit']" type="primary" @click="handleUpdate(scope.row)">详情 </el-button>
						<el-button v-hasPermi="['system:version:remove']" type="danger" @click="handleDelete(scope.row)">删除 </el-button>
					</div>
				</template>
			</el-table-column>
		</el-table>

		<!-- 添加或编辑【请填写功能名称】对话框 -->
		<l-dialog v-model="open" :title="title" ok-button-text="保存" width="600px" :show-button="title !== '详情'" @close="cancel" @formClose="cancel" @formSubmit="submitForm">
			<el-form ref="form" :disabled="title === '详情'" :model="form" :rules="rules" label-width="120px">
				<el-form-item label="版本号：" prop="versionCode">
					<el-input v-model.trim="form.versionCode" onkeyup="value=value.replace(/[^\d]/g,'')" placeholder="请输入版本号" />
				</el-form-item>
				<el-form-item label="版本名称：" prop="versionName">
					<el-input v-model.trim="form.versionName" placeholder="请输入版本名称" />
				</el-form-item>
				<el-form-item label="版本信息：" prop="versionInfo">
					<el-input v-model.trim="form.versionInfo" placeholder="请输入版本信息" />
				</el-form-item>
				<el-form-item label="APK上传：" prop="versionInfo">
					<el-upload :action="uploadFileUrl" :headers="headers" :limit="1" :on-success="uploadFile" class="upload-demo">
						<el-button type="primary">点击上传</el-button>
					</el-upload>
				</el-form-item>

				<el-form-item label="是否强制下载：" prop="updateType">
					<el-select v-model.trim="form.updateType" placeholder="请选择是否强制下载">
						<el-option label="强制" value="forcibly" />
						<el-option label="不强制" value="solicit" />
					</el-select>
				</el-form-item>
				<el-form-item label="备注：" prop="remark">
					<el-input v-model.trim="form.remark" :rows="5" class="!w-[299px]" placeholder="请输入内容" type="textarea" />
				</el-form-item>
			</el-form>
		</l-dialog>
	</l-page>
</template>

<script>
import { addVersion, delVersion, exportVersion, getVersion, listVersion, updateVersion } from '@/api/mall/version';
import { getToken } from '@/utils/auth';
import tableMixins from '@/mixins/table';
export default {
	name: 'Version',
	components: {},
	mixins: [tableMixins],
	data() {
		return {
			uploadFileUrl: process.env.VUE_APP_BASE_API + '/common/upload', // 上传的图片服务器地址
			headers: {
				Authorization: 'Bearer ' + getToken(),
			},
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 【请填写功能名称】表格数据
			versionList: [],
			// 弹出层标题
			title: '',
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				versionCode: null,
				versionName: null,
				versionInfo: null,
				updateType: null,
				downloadUrl: null,
				tenantId: null,
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
		};
	},
	created() {
		this.getList();
	},
	methods: {
		getTime(startTime, endTime) {
			this.queryParams.startTime = startTime;
			this.queryParams.endTime = endTime;
		},
		updateTypeFun(updateType) {
			if (updateType == 'forcibly') {
				return '强制';
			} else {
				return '不强制';
			}
		},
		/** 查询【请填写功能名称】列表 */
		getList() {
			this.loading = true;
			listVersion(this.queryParams)
				.then((response) => {
					this.versionList = response.data.records;
					this.total = response.data.total;
					this.setActionWidth();
				})
				.finally(() => {
					this.loading = false;
				});
		},
		uploadFile(res, file) {
			this.form.downloadUrl = res.data.url;
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				versionCode: null,
				versionName: null,
				versionInfo: null,
				updateType: null,
				downloadUrl: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null,
				remark: null,
				tenantId: null,
			};
			this.resetForm('form');
		},
		statusChange(event, row) {
			this.$request
				.post('system/version/edit', {
					id: row,
					updateType: event,
				})
				.then(() => {
					this.msgSuccess('更新成功');
					this.getList();
				});
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.$refs.child.resetTime();
			this.resetForm('queryForm');
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map((item) => item.id);
			this.single = selection.length !== 1;
			this.multiple = !selection.length;
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = '添加【请填写功能名称】';
		},
		/** 编辑按钮操作 */
		handleUpdate(row) {
			this.reset();
			const id = row.id;
			getVersion(id).then((response) => {
				this.form = response.data;
				this.open = true;
				this.title = '详情';
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs['form'].validate((valid) => {
				if (valid) {
					this.loading = true;
					if (this.form.id != null) {
						updateVersion(this.form).then((response) => {
							this.loading = false;
							this.msgSuccess('编辑成功');
							this.open = false;
							this.getList();
						});
					} else {
						addVersion(this.form).then((response) => {
							this.loading = false;
							this.msgSuccess('新增成功');
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id;
			this.$confirm('是否确认删除版本号为"' + row.versionName + '"的数据项?', '警告', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return delVersion(ids);
				})
				.then(() => {
					this.getList();
					this.msgSuccess('删除成功');
				});
		},
		/** 导出按钮操作 */
		handleExport() {
			const queryParams = this.queryParams;
			this.$confirm('是否确认导出所有【请填写功能名称】数据项?', '警告', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return exportVersion(queryParams);
				})
				.then((response) => {
					this.download(response.msg);
				});
		},
	},
};
</script>
